CXX_LIBRARY_COMPILER_FLAGS = []

REACT_LIBRARY_EXTRA_COMPILER_FLAGS = []

if THIS_IS_FBOBJC:
  include_defs("xplat//configurations/buck/apple/dependency_aggregator_defs")
  inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS
  CXX_LIBRARY_COMPILER_FLAGS = inherited_buck_flags.get_flag_value('compiler_flags')
  REACT_LIBRARY_EXTRA_COMPILER_FLAGS = ['-Wno-shadow', '-Wno-missing-prototypes', '-Wno-global-constructors']

def kwargs_add(base_kwargs, **new_kwargs):
  ret_kwargs = dict(base_kwargs)
  for name, add_value in new_kwargs.iteritems():
    if name in ret_kwargs:
      # Don't use +=, it will modify base_kwargs
      ret_kwargs[name] = ret_kwargs[name] + add_value
    else:
      ret_kwargs[name] = add_value
  return ret_kwargs

if THIS_IS_FBANDROID:
  include_defs('//ReactAndroid/DEFS')

  def react_library(**kwargs):
    kwargs = kwargs_add(
      kwargs,
      compiler_flags = [
        '-Wno-pessimizing-move',
      ],
      deps = [
        '//xplat/folly:molly'
      ])

    cxx_library(
      name = 'bridge',
      **kwargs_add(
        kwargs,
        preprocessor_flags = [
          '-DWITH_JSC_EXTRA_TRACING=1',
          '-DWITH_JSC_MEMORY_PRESSURE=1',
          '-DWITH_REACT_INTERNAL_SETTINGS=1',
          '-DWITH_FB_MEMORY_PROFILING=1',
          '-DWITH_INSPECTOR=1',
        ],
        deps = JSC_DEPS,
        visibility = [
          # TL;DR: If you depend on this target directly, you're gonna have a
          # Bad Time(TM).
          #
          # `facebook::react::JSCExecutor::initOnJSVMThread` (in `:bridge`) does
          # some platform-dependant setup. Exactly what setup to do is
          # determined by some static functors, defined in `Platform.h`, which
          # are initially `nullptr`. On Android, these functors are properly
          # assigned as part of `xreact`'s `JNI_OnLoad`.  By depending directly
          # on the bridge, we can mess up the SO initialisation order, causing
          # `initOnJSVMThread` to be called before the platform-specific hooks
          # have been properly initialised. Bad Times(TM).
          #     -- @ashokmenon (2017/01/03)
          react_native_target('jni/xreact/jni:jni'),
          react_native_xplat_target('cxxreact/...'),
        ],
      )
    )

if THIS_IS_FBOBJC:

  if should_use_dependency_aggregation():
    INSPECTOR_FLAGS = []
  else:
    INSPECTOR_FLAGS = [ '-DWITH_INSPECTOR=1', ]

  def react_library(**kwargs):
    fb_apple_library(
      name = 'bridge',
      header_path_prefix = "cxxreact",
      inherited_buck_flags = STATIC_LIBRARY_IOS_FLAGS,
      frameworks = [
        '$SDKROOT/System/Library/Frameworks/JavaScriptCore.framework',
      ],
      tests = [
        react_native_xplat_target('cxxreact/tests:tests')
      ],
      **kwargs_add(
        kwargs,
        preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS + INSPECTOR_FLAGS,
        deps = [
          '//xplat/folly:molly',
        ],
        visibility = [ 'PUBLIC' ],
      )
    )

cxx_library(
    name = "module",
    compiler_flags = CXX_LIBRARY_COMPILER_FLAGS,
    exported_headers = [
        "CxxModule.h",
        "JsArgumentHelpers.h",
        "JsArgumentHelpers-inl.h",
    ],
    force_static = True,
    header_namespace = "cxxreact",
    labels = ["accounts_for_platform_and_build_mode_flags"],
    visibility = [
        "PUBLIC",
    ],
    xcode_public_headers_symlinks = True,
    deps = [
        "//xplat/folly:molly",
    ],
)

cxx_library(
    name = "samplemodule",
    srcs = ["SampleCxxModule.cpp"],
    compiler_flags = CXX_LIBRARY_COMPILER_FLAGS + [
        "-fno-omit-frame-pointer",
        "-Wall",
        "-Werror",
        "-std=c++1y",
        "-fexceptions",
    ],
    exported_headers = ["SampleCxxModule.h"],
    header_namespace = "",
    soname = "libxplat_react_module_samplemodule.so",
    visibility = [
        "PUBLIC",
    ],
    xcode_public_headers_symlinks = True,
    deps = [
        ":module",
        "//xplat/folly:molly",
    ],
)

CXXREACT_PUBLIC_HEADERS = [
    "CxxNativeModule.h",
    "Executor.h",
    "Instance.h",
    "JSBigString.h",
    "JSBundleType.h",
    "JSCExecutor.h",
    "JSCNativeModules.h",
    "JSIndexedRAMBundle.h",
    "JSModulesUnbundle.h",
    "MessageQueueThread.h",
    "MethodCall.h",
    "ModuleRegistry.h",
    "NativeModule.h",
    "NativeToJsBridge.h",
    "Platform.h",
    "RecoverableError.h",
    "SharedProxyCxxModule.h",
    "SystraceSection.h",
]

react_library(
    srcs = glob(
        ["*.cpp"],
        excludes = ["SampleCxxModule.cpp"],
    ),
    compiler_flags = [
        "-Wall",
        "-fexceptions",
        "-frtti",
        "-std=c++1y",
    ] + REACT_LIBRARY_EXTRA_COMPILER_FLAGS,
    exported_headers = CXXREACT_PUBLIC_HEADERS,
    force_static = True,
    header_namespace = "cxxreact",
    headers = glob(
        ["*.h"],
        excludes = CXXREACT_PUBLIC_HEADERS,
    ),
    preprocessor_flags = [
        "-DLOG_TAG=\"ReactNative\"",
        "-DWITH_FBSYSTRACE=1",
    ],
    xcode_public_headers_symlinks = True,
    deps = [
        ":module",
        "//xplat/fbsystrace:fbsystrace",
        react_native_xplat_target("jschelpers:jschelpers"),
        react_native_xplat_target("microprofiler:microprofiler"),
    ],
)
